package com.anssy.voteballot.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.anssy.voteballot.R;
import com.anssy.voteballot.utils.StatusBarUtils;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * @author jackyChou
 */
public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";
    private int layoutResource;
    public View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        layoutResource = setLayoutResource();
        view = inflater.inflate(layoutResource, container, false);
        initView();
        initData();
        initListener();
        return view;
    }


    public abstract int setLayoutResource();

    public abstract void initView();

    public abstract void initData();

    public abstract void initListener();

    /**
     * 配置toolBar
     *
     * @param title
     * @param activity
     */
    public void configBaseToolBar(String title, final Activity activity) {
        Toolbar toolBar = view.findViewById(R.id.toolbar);
        TextView mTvTitle = view.findViewById(R.id.toolbar_title);
        mTvTitle.setText(title);
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    /**
     * 配置toolBar
     *
     * @param title
     * @param activity
     */
    public void configBaseToolBarNoIcon(String title, Activity activity) {
        TextView mTvTitle = view.findViewById(R.id.toolbar_title);
        mTvTitle.setText(title);

    }

    public RequestBody setRequestBody(String reqJson) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), reqJson);
        return body;
    }

    public RequestBody setRequestJson(Object object) {
        String reqJson = new Gson().toJson(object);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), reqJson);
        return body;
    }

    @Override
    public void onResume() {
        super.onResume();
        StatusBarUtils.setStatusTextColor(true, getActivity());
    }
}
